{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gym\n",
    "import gym_armed_bandits\n",
    "\n",
    "from action_value import sample_average, exponential_regency_weighted_average\n",
    "from agents import ValueAgent\n",
    "from decision_making import RandomPolicy, GreedyPolicy, EpsilonGreedyPolicy, UpperConfidenceBoundPolicy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.09170308,  0.85781046,  0.02511606, -1.36945499, -0.54722494,\n",
       "        0.17091192, -1.03671024, -1.97376597,  1.28215489, -0.60623305])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "env = gym.make('ten-armed-bandits-v0')\n",
    "env.means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "policy = EpsilonGreedyPolicy()\n",
    "action_value_estimation = exponential_regency_weighted_average #sample_average"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<agents.ValueAgent at 0x10610c6d0>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent = ValueAgent(env, policy, action_value_estimation, store_rewards=True, alpha=.75)\n",
    "agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8131869831973225"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iterations = 10000\n",
    "\n",
    "avg_reward = 0\n",
    "\n",
    "for i in range(iterations):\n",
    "    action = agent.act()\n",
    "    reward = env.step(action)[1]\n",
    "    avg_reward += reward\n",
    "    agent.observe(reward)\n",
    "avg_reward /= iterations\n",
    "avg_reward"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.06464696,  0.0204253 , -0.29002282, -0.17892207, -0.49054267,\n",
       "       -0.34053249, -0.59918149, -0.42798752,  0.33152645, -0.28055202])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.Q"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
